package com.learn.http.doc;

import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : Yu.Tan
 * @date : 2023/5/12 15:49
 * @desc : **
 */
public class SignDocUtil {

    public static String makeDocSign(Long timestamp,String appCode,String secretKey){
        String signContent = appCode+ secretKey +  timestamp;
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] encodeData = encoder.encode(signContent.getBytes(StandardCharsets.UTF_8));
        return DigestUtils.sha256Hex(encodeData);
    }

    public static Map<String, String> makeDocSignHeader(String appCode, String secretKey){
        long timestamp = System.currentTimeMillis();
        String sign = makeDocSign(timestamp, appCode, secretKey);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appCode",appCode);
        headerMap.put("timestamp",String.valueOf(timestamp));
        headerMap.put("sign",sign);
        return headerMap;
    }
}
